package com.ksyun.whgc.liweiliang.work1;

import java.util.*;

/**
 * @Deseription
 * @Author lwl
 * @Date 2023/4/19 18:14
 */
public class MyString {

    /**
     * @Description 反转字符串参数
     * @Author lwl
     * @Date 2023/4/19 22:08
     * @param str
     * @return java.lang.String
     * @Test "1234567"
     */
    public String myReverse(String str) {
        if (str == null||str.isEmpty()) {
            return null;
        }
        // StringBuffer 自带反转函数sb.reverse().toString()
        String result = "";
        for (int i = str.length() - 1; i >= 0; i--) {
            result += str.charAt(i);
        }
        return result;
    }


    /**
     * @Description 统计字符串中每个字母出现的次数
     * @Author lwl
     * @Date 2023/4/20 16:35
     * @param str
     * @return java.util.HashMap<java.lang.Character,java.lang.Integer>
     * @Test "abcabcad"
     */
    public HashMap<Character,Integer> statisticeCharCount (String str){

        if (str == null||str.isEmpty()) {
            return null;
        }
        HashMap<Character, Integer> hashMap = new HashMap<>();

        for (int i = 0; i < str.length(); i++) {
            char c = str.charAt(i);
            hashMap.put(c,hashMap.getOrDefault(c,0)+1);
        }
        /*

        char[] chars = str.toCharArray();
        for (char c : chars) {
            if(hashMap.keySet().contains(c)){
                hashMap.put(c,hashMap.get(c) + 1);
            } else {
                hashMap.put(c, 1);
            }
        }*/

        return hashMap;
    }


    /**
     * @Description 出现次数最多的字母及次数
     * @Author lwl
     * @Date 2023/4/20 19:15
     * @param str
     * @return java.util.HashMap<java.util.List<java.lang.Character>,java.lang.Integer>
     */
    public HashMap<List<Character>,Integer> printCharByMaxCharCount(String str){
        if (str == null||str.isEmpty()) {
            return null;
        }
        
        List<Character> charlist = new ArrayList<>();
        HashMap<Character, Integer> hashMap = new HashMap<>();
        HashMap<List<Character>,Integer> listIntegerHashMap = new HashMap<>();

        //获取元素
        hashMap = statisticeCharCount(str);
        //重复出现最大值
        int value = getMaxValue(hashMap);
        //返回value的key
        charlist = getKeyList(hashMap, value);

        listIntegerHashMap.put(charlist, value);

        return listIntegerHashMap;
    }

    /**
     * @Description 获得传入Map中Value的最大值
     * @Author lwl
     * @Date 2023/4/20 19:20
     * @param hashMap
     * @return int
     */
    private int getMaxValue(HashMap hashMap){

        int flag = 0;
        Collection<Integer> values = hashMap.values();
        Iterator<Integer> iterator = values.iterator();
        while(iterator.hasNext()){
            int t = iterator.next();
            if(flag <= t) {
                flag = t;
            }
        }
        return flag;
    }

    /**
     * @Description 通过 value 找所有的 key
     * @Author lwl
     * @Date 2023/4/20 19:21
     * @param hashMap 出入Map
     * @param value   传入value值
     * @return java.util.List<java.lang.Character>
     */
    private List<Character> getKeyList(HashMap hashMap,int value){
        
        List<Character> keylist = new ArrayList<>();

        for ( Object obj : hashMap.keySet()) {
            if(hashMap.get(obj).equals(value)){
                keylist.add((Character) obj);
            }
        }
        return keylist;
    }


    /**
     * @Description printCharByMaxCharCount函数优化版本，直接用一个循环找到
     * @Author lwl
     * @Date 2023/4/20 23:04
     * @param str
     * @return java.util.HashMap<java.util.List<java.lang.Character>,java.lang.Integer>
     */
    public HashMap<List<Character>,Integer> printCharByMaxCharCountPro(String str){
        if (str == null || str.isEmpty()) {
            return null;
        }

        HashMap<Character, Integer> charCount = new HashMap<>();
        HashMap<List<Character>,Integer> listIntegerHashMap = new HashMap<>();
        List<Character> maxChar = new ArrayList<>();
        charCount = statisticeCharCount(str);

        int maxCount = 0;

        for (Character c : charCount.keySet()) {
            if (charCount.get(c) > maxCount) {
                maxChar.clear();
                maxChar.add(c);
                maxCount = charCount.get(c);

            }
        }

        listIntegerHashMap.put(maxChar, maxCount);
        return  listIntegerHashMap;
    }


    /**
     * @Description 不含有重复字符的最长子串
     * 滑动窗口解决 [l,r]，但是这里如果长度相同只会返回第一个字串
     * aaabbbccc  ==> 返回ab bc  但是只会返回ab 需要改进
     * @Author lwl
     * @Date 2023/4/20 19:55
     * @param str
     * @return java.lang.String
     */
    public String nonredundantString(String str){
            String subString = "";
            int l = 0 ,r = 0;
            HashMap<Character,Integer> characterIntegerHashMap = new HashMap<>();

            while( r < str.length()){
                char c = str.charAt(r);
                if( characterIntegerHashMap.containsKey(c) && characterIntegerHashMap.get(c) >= l){
                    l = characterIntegerHashMap.get(c)+1;
                }
                characterIntegerHashMap.put(c, r);
                r++;
                if (r - l > subString.length()){
                    subString = str.substring(l, r);
                }
            }
            return subString;
    }



}
